/**
 * @author
 * @description 13位时间戳转换
 * @param time
 * @returns {string}
 */
export const tenBitTimestamp = (date) => {
	let _date = new Date(parseInt(date));
	let y = _date.getFullYear();
	let m = _date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	let d = _date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = _date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let minute = _date.getMinutes();
	let second = _date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	// console.log( y + '-' + m + '-' + d + ' ' + '　' + h + ':' + minute + ':' + second) 
	let dates = m + '-' + d + ' ';
	return dates;
}

/**
 * @author
 * @description 国际时间转年月日
 * @param time
 * @returns {string}
 */
export const getTime = (days) => {
	var d = new Date();
	d.setTime(d.getTime() - days * 24 * 3600 * 1000);
	return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
}

/**
 * @author
 * @description 2022-01-18T00:00:00Z => 2022-01-18
 * @param time
 * @returns {string}
 */
export const resetTime = (date) => {
    var json_date = new Date(date).toJSON();
	let newDate = new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
	let cDate = newDate.split(' ')[0]
    return cDate
}

/**
 * @author
 * @description 2022-01-18T00:00:00Z => yyyy-MM-dd HH:mm:ss
 * @param time
 * @returns {string}
 */
export function dateFormat (date, format = 'yyyy-MM-dd HH:mm:ss' ,AddDayCount=0) {
  date = new Date(date);
  date.setDate(date.getDate() + AddDayCount)
  var o = {
    'M+': date.getMonth() + 1, //month
    'd+':date.getDate(), //day
    'H+': date.getHours(), //hour
    'm+': date.getMinutes(), //minute
    's+': date.getSeconds(), //second
    'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
    'S': date.getMilliseconds() //millisecond
  };
  if (/(y+)/.test(format))
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp('(' + k + ')').test(format))
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));

  return format;
}

/**
 * 计算文本字节大小
 * @param {*} str 
 * @param {*} charset 
 */
export function strSize(str, charset='utf-8') {
	let total = 0;
	charset = charset?.toLowerCase() || '';
	for (let i = 0; i < str.length; i++) {
		let charCode = str.charCodeAt(i);
		if (charset === 'utf-16' || charset === 'utf16') {
			total += charCode <= 0xffff ? 2 : 4;
		} else {
			if (charCode <= 0x007f) {
				total += 1;
			} else if (charCode <= 0x07ff) {
				total += 2;
			} else if (charCode <= 0xffff) {
				total += 3;
			} else {
				total += 4;
			}
		}
	}
	return total;
}

/**
 * 计算两个时间戳之间的间隔
 * @param {Object} startTime
 * @param {Object} endTime
 */
export function calculateDiffTime(startTime, endTime) {
  // console.log(startTime)
  // console.log(endTime)
  let runTime = parseInt((endTime - startTime) / 1000);
  var year = Math.floor(runTime / 86400 / 365);
  runTime = runTime % (86400 * 365);
  var month = Math.floor(runTime / 86400 / 30);
  runTime = runTime % (86400 * 30);
  var day = Math.floor(runTime / 86400);
  runTime = runTime % 86400;
  var hour = Math.floor(runTime / 3600);
  runTime = runTime % 3600;
  var minute = Math.floor(runTime / 60);
  runTime = runTime % 60;
  var second = runTime;
  // console.log(`相差${year}年${month}月${day}天${hour}小时${minute}分${second}秒`);
  
  if (year > 0) {
    day = day + (day * 365)
  }
  if (month > 0) {
    day = day + (month * 30)
  }
  if (day > 0) {
    hour = hour + (day * 24)
  }
  if (day < 10) {
    day = '0' + day
  }
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minute < 10) {
    minute = '0' + minute
  }
  if (second < 10) {
    second = '0' + second
  }
  // return hour + ':' + minute + ':' + second;
  // return year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + second;
  let timeData = {
  	  hour: hour,
  	  minute: minute,
  	  second: second
  }
  return timeData
}

/**
 * 获取当前年月日
 */
export function getNowDate(type){
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth()+1;
	let day = date.getDate();
	let hour = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();
	if (month <= 9) {
		month = '0' + month
	}
	if (day <= 9) {
		day = '0' + day
	}
	if (hour <= 9) {
		hour = '0' + hour
	}
	if (minute <= 9) {
		minute = '0' + minute
	}
	if (second <= 9) {
		second = '0' + second
	}
	let time = ''
	if (type === 'hh:mm:ss') {
		time = `${year}-${month}-${day} ${hour}:${minute}:${second}`
	} else {
		time = `${year}-${month}-${day}`
	}
	return time
}


export function getLinkContent(content){
  if (!content) {
	  return "";
  }
  let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g;
  content = content.replace(urlPattern, function (match) {
	  var href = match;
	  if (match.indexOf("http") == -1) {
		  href = "http://" + match;
	  }
	  return "<a target=\"_blank\" href=\"" + href + "\" style=\"text-decoration: none;color: #26bbff;\">" + match + "</a>";
  });
  return content;
}
export function goBack(delta) {
	var currrentPage = getCurrentPages();
	if(delta === -1 || currrentPage.length == 1){
		if(appSource() == 'ios'){
			try{
				window.webkit.messageHandlers.navBack.postMessage({})
			}catch(e){
				//TODO handle the exception
			}
			try{
				window.webkit.messageHandlers.back.postMessage({})
			}catch(e){
				//TODO handle the exception
			}
		}else{
			try{
				window.android.back()
			}catch(e){
				//TODO handle the exception
			}
			try{
				window.android.navBack()
			}catch(e){
				//TODO handle the exception
			}
		}
	}else{
		uni.navigateBack({
			delta: delta||1
		});
	}
	
}
